//
//  ZYShareAlertView.m
//  SuiTong
//
//  Created by 石志愿 on 2020/9/29.
//  Copyright © 2020 石志愿. All rights reserved.
//

#import "ZYShareAlertView.h"
#import "Masonry.h"

@interface ZYSharePlatformCell : UICollectionViewCell

@property (nonatomic, strong) ZYThirdSharePlatformInfo *info;

@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UILabel *nameLabel;

@end

@implementation ZYSharePlatformCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setSubViews];
    }
    return self;
}

- (void)setSubViews {
    UIImageView *iconView = [[UIImageView alloc]init];
    self.iconView = iconView;
    [self.contentView addSubview:iconView];
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(50);
        make.top.mas_equalTo(0);
        make.centerX.equalTo(self.contentView);
    }];
    
    UILabel *nameLabel = [[UILabel alloc]init];
    self.nameLabel = nameLabel;
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.font = kFont(14);
    nameLabel.textColor = kHexColor(0x333333);
    [self.contentView addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.mas_equalTo(0);
    }];
}

- (void)setInfo:(ZYThirdSharePlatformInfo *)info {
    _info = info;
    self.iconView.image = [UIImage imageNamed:info.icon];
    self.nameLabel.text = info.title;
}

@end

@interface ZYShareAlertView ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) ZYCollectionView *collectionView;
@property (nonatomic, strong) UIButton *closeButton;

@property (nonatomic, strong) NSMutableArray *platforms;

@end

@implementation ZYShareAlertView

- (instancetype)init
{
    return [self initWithContentHeight:208 style:ZYAlertViewStyleBottom];
}

- (instancetype)initWithContentHeight:(CGFloat)contentHeight style:(ZYAlertViewStyle)style {
    self = [super initWithContentHeight:208 style:ZYAlertViewStyleBottom];
    if (self) {
        [self setSubViews];
        [self setUpPlatforms];
    }
    return self;
}

- (void)setSubViews {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(60, 76);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 12;
    layout.sectionInset = UIEdgeInsetsMake(16, 16, 26, 16);
    ZYCollectionView *collectionView = [[ZYCollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 118) collectionViewLayout:layout];
    self.collectionView = collectionView;
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [collectionView registerClass:ZYSharePlatformCell.class forCellWithReuseIdentifier:@"ZYSharePlatformCell"];
    [self.contentView addSubview:collectionView];
    
    UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.closeButton = closeButton;
    closeButton.frame = CGRectMake(16, collectionView.bottom+10, kScreenWidth-32, 40);
    [closeButton setTitle:@"取消" forState:UIControlStateNormal];
    [closeButton setTitleColor:kHexColor(0x333333) forState:UIControlStateNormal];
    closeButton.titleLabel.font = kFont(16);
    [closeButton addTarget:self action:@selector(dismissView) forControlEvents:UIControlEventTouchUpInside];
    closeButton.backgroundColor = kWhiteColor;
    [self.contentView addSubview:closeButton];
}

- (void)setUpPlatforms {
    [self.platforms addObject:[[ZYThirdSharePlatformInfo alloc]initWithPlatform:ZYSharePlatformQQ]];
    
    [self.platforms addObject:[[ZYThirdSharePlatformInfo alloc]initWithPlatform:ZYSharePlatformQzone]];

    if ([ZYShareManager shared].installWeChat) {
        [self.platforms addObject:[[ZYThirdSharePlatformInfo alloc]initWithPlatform:ZYSharePlatformWXSession]];
        [self.platforms addObject:[[ZYThirdSharePlatformInfo alloc]initWithPlatform:ZYSharePlatformWXTimeline]];
    }
    [self.platforms addObject:[[ZYThirdSharePlatformInfo alloc]initWithPlatform:ZYSharePlatformWeiBo]];
    
    [self.platforms addObject:[[ZYThirdSharePlatformInfo alloc]initWithPlatform:ZYSharePlatformCopylink]];
    [self.collectionView reloadData];
}

//MARK: UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.platforms.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ZYSharePlatformCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZYSharePlatformCell" forIndexPath:indexPath];
    cell.info = self.platforms[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    ZYThirdSharePlatformInfo *info = self.platforms[indexPath.row];
    if (self.didSelectPlatform) {
        self.didSelectPlatform(info.platform);
    } else {
        if (info.platform == ZYSharePlatformCopylink) {
            [[ZYShareManager shared] copylink:self.shareInfo.url];
            if (self.completion) {
                self.completion();
            }
        } else {
            [[ZYShareManager shared] shareWebPage:self.shareInfo platform:info.platform completion:^(ZYShareStatus code) {
                if (code == ZYShareStatusSuccess) {
                    if (self.completion) {
                        self.completion();
                    }
                }
            }];
        };
    }
    [self dismissView];
}

- (NSMutableArray *)platforms {
    if (!_platforms) {
        _platforms = [NSMutableArray arrayWithCapacity:0];
    }
    return _platforms;
}
@end
